﻿using HomagChina.CCHCS.DataTypeModel;
using Proxy;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using static HomagChina.CCHCS.ProcessResourceInternal;

namespace HomagChina.CCHS.ViewModels
{
    public class ParcelLayerCreator
    {
        private const double ContainersWidth = 1660;
        private const double ContainersHeight = 735;

        private const double RealLengthMm = 3000;
        private const double RealWidthMm = 1000;

        public Canvas MainCanvas { get; }
        public ObservableCollection<LayerInfo> Layers { get; }

        public ParcelLayerCreator(Canvas mainCanvas, ObservableCollection<LayerInfo> layers)
        {
            MainCanvas = mainCanvas;
            Layers = layers;
            InitializeCanvas();
        }

        private void InitializeCanvas()
        {
            MainCanvas.Children.Clear();

            if (Layers == null || !Layers.Any()) return;

            var layerGroups = Layers.GroupBy(x => x.RSLayerNum).OrderBy(g => g.Key).ToList();

            int totalGroups = layerGroups.Count;

            // 固定每行2个容器
            const int itemsPerRow = 2;
            int totalRows = (int)Math.Ceiling(totalGroups / (double)itemsPerRow);

            double containerWidth = (ContainersWidth - 30) / itemsPerRow;
            double containerHeight = (ContainersHeight - 30) / totalRows;

            // 创建所有子容器
            for (int i = 0; i < totalGroups; i++)
            {
                int row = i / itemsPerRow;
                int col = i % itemsPerRow;

                var subCanvas = CreateSubCanvas(actualWidth: containerWidth, actualHeight: containerHeight, row: row, col: col);

                DrawGroupContent(subCanvas, layerGroups[i].ToList());
                MainCanvas.Children.Add(subCanvas);
            }
        }

        private Canvas CreateSubCanvas(double actualWidth, double actualHeight, int row, int col)
        {
            var canvas = new Canvas
            {
                Width = actualWidth,
                Height = actualHeight,
                Background = GetHexadecimalColor("#819cbe"),
                Tag = new MmScaleFactors(xScale: actualWidth / RealLengthMm, yScale: actualHeight / RealWidthMm)
            };
            Canvas.SetLeft(canvas, col * (actualWidth + 15));
            Canvas.SetTop(canvas, row * (actualHeight + 15));

            return canvas;
        }

        private void DrawGroupContent(Canvas subCanvas, List<LayerInfo> layers)
        {
            var scale = (MmScaleFactors)subCanvas.Tag;
            var lbl = new Label
            {
                Content = $"Layer {layers[0].RSLayerNum}",
                FontSize = 18,
                Opacity = 0.8,
                FontWeight = FontWeights.Bold,
                Foreground = GetHexadecimalColor("#F6C5A9")
            };
            Canvas.SetTop(lbl, 5);
            Canvas.SetRight(lbl, 5);
            subCanvas.Children.Add(lbl);

            foreach (var layer in layers)
            {
                double displayWidth = layer.PackRt90 ? (double)layer.Width * scale.X : (double)layer.Length * scale.X;
                double displayHeight = layer.PackRt90 ? (double)layer.Length * scale.Y : (double)layer.Width * scale.Y;

                var rect = new Rectangle
                {
                    Width = displayWidth,
                    Height = displayHeight,
                    Fill = GetHexadecimalColor("#ff9300"),
                    Stroke = GetHexadecimalColor("#000000"),
                    StrokeThickness = 0.5,
                    RadiusX = 2,
                    RadiusY = 2
                };

                double originX = ((double)layer.CalCenterX * scale.X) - (displayWidth / 2);
                double originY = ((double)layer.RSCenterY * scale.Y) - (displayHeight / 2);
                Canvas.SetLeft(rect, originX);
                Canvas.SetTop(rect, originY);

                subCanvas.Children.Add(rect);
            }
        }

        private class MmScaleFactors
        {
            public double X { get; }
            public double Y { get; }

            public MmScaleFactors(double xScale, double yScale)
            {
                X = xScale;
                Y = yScale;
            }
        }
    }
}